//
//  BarView.m
//  FindJob
//
//  Created by 233 on 16/3/7.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "BarView.h"
#define kTitleLabelHeight 30

@implementation BarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _grade = 0;
        _barColor = [UIColor grayColor];
        // 添加子视图
        [self.contentView addSubview:self.subTitleLabel];
        [self addSubview:self.contentView];
        [self addSubview:self.titleLabel];
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)updateViews{
    
    CGFloat height = (self.frame.size.height - kTitleLabelHeight) * self.grade;
    
    self.contentView.frame = CGRectMake(0, self.frame.size.height - height, self.frame.size.height, height);
    self.titleLabel.frame = CGRectMake(0, self.contentView.frame.origin.y - kTitleLabelHeight, self.frame.size.width, kTitleLabelHeight);
    
    CGRect frame = self.frame;
    self.frame = CGRectOffset(frame, 0, height);
    [UIView animateWithDuration:1.0f animations:^{
        self.frame = frame;
    }];
    
}
#pragma mark -- setters
- (void)setBarColor:(UIColor *)barColor{
    if (barColor ==nil) {
        return;
    }
    _barColor = barColor;
    _contentView.backgroundColor = barColor;
}

#pragma mark -- getter
- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 44, self.frame.size.width, 44)];
        _contentView.backgroundColor = self.barColor;
    }
    return _contentView;
}
- (UILabel *)subTitleLabel{
    if (!_subTitleLabel) {
        _subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 20)];
        _subTitleLabel.font = [UIFont systemFontOfSize:14];
        _subTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _subTitleLabel;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.contentView.frame.origin.y - kTitleLabelHeight, self.frame.size.width, kTitleLabelHeight)];
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
@end
